/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CredentialRequired.java
 *
 * Created on 16-mar-2010, 1.53.45
 */
package freimapgsoc;

import java.sql.Connection;
import com.mysql.jdbc.DatabaseMetaData;
import com.mysql.jdbc.Statement;
import java.awt.Color;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Stefano
 */
public class CredentialRequired extends javax.swing.JFrame {

    /** Creates new form CredentialRequired */
    public CredentialRequired() {
        initComponents();
    }

    /**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 * @return 
	 */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        userText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        connectButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        passwordText = new javax.swing.JPasswordField();
        logLabel = new javax.swing.JLabel();
        hostText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        onlydataNode = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        portText = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        dbText = new javax.swing.JComboBox();
        loadDb = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MySql Database Credential");
        setResizable(false);
        setSize(new java.awt.Dimension(200, 200));

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        jLabel1.setText("UserName:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        jLabel2.setText("Password:");
        jLabel2.setName("jLabel2"); // NOI18N

        userText.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        userText.setName("userText"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 8));
        jLabel4.setText("For security reason you cannot store User/Pass");
        jLabel4.setName("jLabel4"); // NOI18N

        connectButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freimapgsoc/resources/statuok.png"))); // NOI18N
        connectButton.setText("Connect");
        connectButton.setName("connectButton"); // NOI18N
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freimapgsoc/resources/close2.png"))); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        passwordText.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        passwordText.setName("passwordText"); // NOI18N

        logLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        logLabel.setText(" ");
        logLabel.setName("logLabel"); // NOI18N

        hostText.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        hostText.setName("hostText"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        jLabel5.setText("Host:");
        jLabel5.setName("jLabel5"); // NOI18N

        onlydataNode.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        onlydataNode.setText("Only Node Data (you cannot see links)");
        onlydataNode.setName("onlydataNode"); // NOI18N

        jLabel6.setText(":");
        jLabel6.setName("jLabel6"); // NOI18N

        portText.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        portText.setText("3306");
        portText.setName("portText"); // NOI18N

        jLabel7.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        jLabel7.setText("Avaible Database:");
        jLabel7.setName("jLabel7"); // NOI18N

        dbText.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        dbText.setName("dbText"); // NOI18N

        loadDb.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freimapgsoc/resources/download.png"))); // NOI18N
        loadDb.setText("Load Avaible Databse");
        loadDb.setName("loadDb"); // NOI18N
        loadDb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDbActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Lucida Grande", 0, 12)); // NOI18N
        jButton1.setText("Example");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(25, 25, 25)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel7)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(dbText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 197, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(46, 46, 46)
                                .add(onlydataNode))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                        .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 189, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(layout.createSequentialGroup()
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .add(jLabel1))
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                                .add(hostText)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, userText)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(jLabel6)))
                                    .add(jLabel2))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(portText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(15, 15, 15))
                    .add(layout.createSequentialGroup()
                        .add(2, 2, 2)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(loadDb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 166, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 101, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(connectButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(logLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .add(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(48, 48, 48)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(hostText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5)
                    .add(jLabel6)
                    .add(portText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(userText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1))
                .add(2, 2, 2)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(dbText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 4, Short.MAX_VALUE)
                .add(onlydataNode)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(logLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(connectButton)
                    .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(loadDb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(71, 71, 71))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        new Start().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    public void findDatabase(String host, String port, String username, String password, String database, boolean onlyNodeData) throws SQLException, InterruptedException {
        try {
            logLabel.setText("Getting connection...");
            Thread.sleep(1000);
            Class.forName("com.mysql.jdbc.Driver");
            c = (Connection) DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
            if (!c.isClosed()) {
                logLabel.setForeground(Color.green);
                logLabel.setText("Connected!");
                Thread.sleep(3000);
                new MysqlDataSource(host, username, password, database, port, onlyNodeData).init();
                this.dispose();
            }
        } catch (Exception ex) {
            logLabel.setForeground(Color.red);
            logLabel.setText(ex.getMessage());
            System.out.println(ex.getMessage());
            System.out.println(ex.getCause());
            Logger.getLogger(CredentialRequired.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        host = hostText.getText();
        port = portText.getText();
        username = userText.getText();
        password = passwordText.getText();
        if (dbText.getSelectedItem().equals(null)) {
            logLabel.setForeground(Color.red);
            logLabel.setText("Select a valid Database!");
        } else {
            database = dbText.getSelectedItem().toString();
        }
        if (host.length() < 2) {
            if (port.length() == 0) {
                port = "3306";
            }
            logLabel.setForeground(Color.red);
            logLabel.setText("Add host address and port number!");
        } else {
            try {
                findDatabase(host, port, username, password, database, onlydataNode.isSelected());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }//GEN-LAST:event_connectButtonActionPerformed

    private void loadDbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDbActionPerformed
        try {
            host = hostText.getText();
            port = portText.getText();
            username = userText.getText();
            password = passwordText.getText();
            if (host.length() < 2) {
                if (port.length() == 0) {
                    port = "3306";
                }
                logLabel.setForeground(Color.red);
                logLabel.setText("Add host address and port number!");
            } else {
                Class.forName("com.mysql.jdbc.Driver");
                c = (Connection) DriverManager.getConnection("jdbc:mysql://" + host + ":" + port, username, password);
                if (!c.isClosed()) {
                    stmt = (Statement) c.createStatement();
                    rss = stmt.executeQuery("SHOW DATABASES");
                    int i = 0;
                    while (rss.next()) {
                        dbText.insertItemAt(rss.getString("Database"), i);
                        i++;
                        dbText.setSelectedIndex(0);
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            logLabel.setForeground(Color.red);
            logLabel.setText(ex.getMessage());
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_loadDbActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        userText.setText("root");
        passwordText.setText("CiscoSte5785");
        hostText.setText("127.0.0.1");
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CredentialRequired().setVisible(true);
            }
        });
    }
     Connection c = null;
    Statement stmt = null;
    ResultSet rss = null;
    String host;
    String username;
    String pass;
    String password;
    String database;
    String port;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton connectButton;
    private javax.swing.JComboBox dbText;
    private javax.swing.JTextField hostText;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JButton loadDb;
    private javax.swing.JLabel logLabel;
    private javax.swing.JCheckBox onlydataNode;
    private javax.swing.JPasswordField passwordText;
    private javax.swing.JTextField portText;
    private javax.swing.JTextField userText;
    // End of variables declaration//GEN-END:variables
}
